package com.changgou.file.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.file.util.FastDFSClient;
import com.changgou.file.util.FastDFSFile;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/file")
public class FileController {
// 接收参数
    @PostMapping("/upload")
    public Result upload(MultipartFile file)throws Exception{
        // 获取文件内容，文件名称，文件后缀
        byte[] bytes = file.getBytes();
        String originalFilename = file.getOriginalFilename();
        String spc = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

        FastDFSFile fastDFSFile = new FastDFSFile(originalFilename, bytes, spc);
        String[] upload = FastDFSClient.upload(fastDFSFile);

        String address = FastDFSClient.getTrackerUrl()+upload[0]+"/"+upload[1];
        return new Result(true, StatusCode.OK,"上传文件成功",address);
    }
    //
}
